var skill = {
	save: function() {
		delete this.user_info;
		db.update(db_table_name, this._id, this, true);
	},
	//商家销毁市代
	destroy: function() {
		var bus = BusCon.get(this.bus_id);
		bus._removePurchase(this._id);
		db.remove(db_table_name, this._id)
	},
	setInfo: function(info, is_no_save) {
		this.厂商 = String(info.厂商 || "").trim();
		if (!info.品名) {
			throwCusError("原材料名称不可为空")
		}
		this.品名 = String(info.品名).trim();
		this.单位 = String(info.单位 || "个").trim();
		this.单价 = parseInt(info.单价, 10);
		if (!this.单价) {
			throwCusError("原材料单价必须有一定额度")
		} else if (this.单价 < 0) {
			throwCusError("原材料单价额度有误")
		}
		// 库存
		if (info.hasOwnProperty("num")) {
			info.num = parseInt(info.num);
			if (info.num < 0) {
				throwCusError("原材料库存数量有误")
			}
			this.num = info.num
		} else {
			this.num = ~~this.num;
		}
		is_no_save || this.save();
	},
	equal: function(info) {
		return this.厂商 === info.厂商 &&
			this.品名 === info.品名 &&
			this.单位 === info.单位 &&
			this.单价 == info.单价;
	},
	like: function(info) {
		if (info.厂商) {
			if (this.厂商.indexOf(info.厂商) === -1) {
				return false
			}
		}
		if (info.品名) {
			if (this.品名.indexOf(info.品名) === -1) {
				return false
			}
		}
		if (info.单位) {
			if (this.单位.indexOf(info.单位) === -1) {
				return false
			}
		}
		if (info.单价 /*||info.单价===0*/ ) { //单价不可为0
			if (this.单价 != info.单价) {
				return false
			}
		}
		return true
	},
	// 非安全，库存
	_addNum: function(num) {
		num = ~~num;
		if (num <= 0) {
			throwCusError("原材料新增库存数量有误")
		}
		this.num += num;
		this.save();
	},
	// 非安全，消耗材料进行转换
	_useNum: function(num, log_msg) {
		num = parseFloat(num) || 0;
		if (num <= 0) {
			throwCusError("原材料消耗库存数量有误")
		}
		var logs = this.logs || (this.logs = []);
		logs.unshift({
			num: num,
			log: log_msg,
			time: +new Date
		});
		this.num -= num;
		this.save();
	}
};

module.exports = skill.multiInherits({});

var db = require("../db/index");
var BusPurchaseCon = require("../bus.purchase/_con");
var UserCon = require("../user/_con");
var BusCon = require("../bus/_con");
var GoodsCon = require("../goods/_con");
var AchCon = require("../ach/_con");
var BusMACon = require("../bus.MA/_con");
var BusUserBindCon = require("../bus_user_bind/_con");
var BusShopCon = require("../bus.shop/_con");
var BuffCon = require("../buff/_con");
var config = require("../_include/config");
var data_cut = require("../_include/data_cut");
var $Array = require("../_include/$.Array");
var $Object = require("../_include/$.Object");
var helper = require("../_include/helper");
var db_table_name = config.dbname.busMaterial;
var db_id_prefix = config.dbIdPrefix.busMaterial;;